//
//  RouteInfoMessageCell.swift
//  Cycling
//
//  Created by CSS on 4/17/23.
//

import UIKit

class RouteInfoMessageCell: UITableViewCell, UITextViewDelegate {
    
    
    @IBOutlet private weak var inputField: UITextView!
    
    @objc public var editModel: CyclingRouteEditModel? {
        didSet {
            contentString = editModel?.contentString
        }
    }
    private var contentString: String? {
        didSet {
            inputField.text = contentString
            reloadTextViewPlaceholder(inEditingStatus: inputField.isFirstResponder)
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        selectionStyle = .none
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
    
    func textViewDidBeginEditing(_ textView: UITextView) {
        reloadTextViewPlaceholder(inEditingStatus: true)
    }
    
    func textViewDidChange(_ textView: UITextView) {
        editModel?.contentString = textView.text
    }
    
    func textViewDidEndEditing(_ textView: UITextView) {
        reloadTextViewPlaceholder()
    }
    
    private func reloadTextViewPlaceholder(inEditingStatus editingStatus: Bool = false) {
        if editingStatus {
            inputField.textColor = .black
            inputField.text = editModel?.contentString
            return
        }
        if editModel?.contentString == nil || editModel!.contentString.count == 0 {
            inputField.textColor = .gray
            inputField.text = "请输入路线介绍"
            return
        }
        inputField.text = editModel?.contentString
        inputField.textColor = .black
    }
    
}
